<?
	include "mysql.php";
	
class graphData
{
	var $mysql ;
	
	function graphData()
	{
		$this->mysql = new MySQL() ;
	}
	
	function queryHour($client_id,$device)
	{
		$t1 = strtotime("now");
		$t2 = strtotime("-1 hour",$t1);
		$date1 = date("Y-m-d H:i:s",$t2);
		$date2 = date("Y-m-d H:i:s",$t1);
		$query = "select usedPercent from history where client_id = $client_id AND device = '$device'
					AND `datetime` > '$date1' AND `datetime` < '$date2' ";
		return $query ;
	}
	
	function queryDay($client_id,$device)
	{
		$t1 = strtotime("now");
		$t2 = strtotime("-1 day",$t1);
		$date1 = date("Y-m-d H:i:s",$t2);
		$date2 = date("Y-m-d H:i:s",$t1);
		$query = "select usedPercent from history where client_id = $client_id AND device = '$device'
					AND `datetime` > '$date1' AND `datetime` < '$date2'";
		return $query ;
	}
	
	function queryWeek($client_id,$device)
	{
		/*group by hour*/
		$t1 = strtotime("now");
		$t2 = strtotime("-7 day",$t1);
		$date1 = date("Y-m-d H:i:s",$t2);
		$date2 = date("Y-m-d H:i:s",$t1);
		$query = "SELECT max(usedPercent) FROM `history`
				WHERE client_id ='$client_id' AND device ='$device' 
				AND `datetime` > '$date1' AND `datetime` < '$date2'
				GROUP BY hour(`datetime`),date(`datetime`)
				ORDER BY `datetime`" ;
		return $query ;
	}
	
	function queryMonth($client_id,$device)
	{
		/*group by hour*/
		$t1 = strtotime("now");
		$t2 = strtotime("-1 month",$t1);
		$date2 = date("Y-m-d H:i:s",$t1);
		$date1 = date("Y-m-d H:i:s",$t2);
		$query = "SELECT max(usedPercent) FROM `history`
				WHERE client_id = '$client_id' AND device ='$device' 
				AND `datetime` > '$date1' AND `datetime` < '$date2'
				GROUP BY hour(`datetime`),date(`datetime`)
				ORDER BY `datetime`" ;
		return $query ;
	}

	function queryYear($client_id,$device)
	{
		/*group by day*/
		$t1 = strtotime("now");
		$t2 = strtotime("-1 year",$t1);
		$date2 = date("Y-m-d H:i:s",$t1);
		$date1 = date("Y-m-d H:i:s",$t2);
		$query = "SELECT max(usedPercent) FROM `history`
				WHERE client_id ='$client_id' AND device ='$device'
				AND `datetime` > '$date1' AND `datetime` < '$date2'
				GROUP BY date(`datetime`)
				ORDER BY `datetime` ";
		return $query ;
	}
	function inHour($client_id,$device,$freq){
		$i = 0;
		$query = $this->queryHour($client_id,$device) ;
		$result = $this->mysql->execute($query);
		while ($row = mysql_fetch_row($result))
			$arr[$i++] = $row[0] ;
		$ans = $this->sampling($arr,$freq);
		return $ans ;
	}
	
	function inDay($client_id,$device,$freq){
		$i = 0;
		$query = $this->queryDay($client_id,$device) ;
		$result = $this->mysql->execute($query);
		while ($row = mysql_fetch_row($result))
			$arr[$i++] = $row[0] ;
		$ans = $this->sampling($arr,$freq);
		return $ans ;
	}
	
	function inWeek($client_id,$device,$freq){
		$i = 0;
		$query = $this->queryWeek($client_id,$device) ;
		$result = $this->mysql->execute($query);
		while ($row = mysql_fetch_row($result))
			$arr[$i++] = $row[0] ;
		$ans = $this->sampling($arr,$freq);
		return $ans ;
	}
	
	function inMonth($client_id,$device,$freq){
		$i = 0;
		$query = $this->queryMonth($client_id,$device) ;
		$result = $this->mysql->execute($query);
		while ($row = mysql_fetch_row($result))
			$arr[$i++] = $row[0] ;
		$ans = $this->sampling($arr,$freq);
		return $ans ;
	}
	function inYear($client_id,$device,$freq){
		$i = 0;
		$query = $this->queryYear($client_id,$device) ;
		$result = $this->mysql->execute($query);
		while ($row = mysql_fetch_row($result))
			$arr[$i++] = $row[0] ;
		$ans = $this->sampling($arr,$freq);
		return $ans ;
	}
	private function sampling($arr,$freq){
		if (($freq >1000) || ($freq <1))
			$freq = 100 ;
		$ans = array() ;
		$numrows = sizeof($arr) ;
		$step = $numrows /$freq ;
		echo "sampling :".$freq." : step".$step." = ".$numrows."/".$freq."<br>";
		for($i=0;$i<$freq;$i++)
		{
			$c = $i * $step ;
			$ans[$i] = $arr[floor($c)] ;
		}
		return $ans ;
	}
}
/*
echo "<h3>Test case to view query Date and Group By(comment when in use na ja)</h3>";

$client_id = 2 ;
$device = "/dev/sda1" ;
$freq = 20 ;
$mysql = new MySQL() ;
$g = new graphData() ;

if (isset($_GET["client_id"]))
	$client_id = $_GET["client_id"] ;
if (isset($_GET["device"]))
	$device = $_GET["device"] ;
if (isset($_GET["freq"]))
	$freq = $_GET["freq"];
echo "Parameter List :client_id = $client_id ,device = $device ,freq =$freq <br>";

echo "<b>Query List</b><br>";
echo "<b>Query Hour:</b><br>".$g->queryHour($client_id,$device)."<br>" ;
echo "<b>Query Day:</b><br>".$g->queryDay($client_id,$device)."<br>" ;
echo "<b>Query Week:</b><br>".$g->queryWeek($client_id,$device)."<br>" ;
echo "<b>Query Month:</b><br>".$g->queryMonth($client_id,$device)."<br>";
echo "<b>Query Year:</b><br>".$g->queryYear($client_id,$device)."<br>";

echo "<br><h4>affected rows list :</h4>";
$result = $mysql->execute($g->queryHour($client_id,$device)) ;
$d = mysql_num_rows($result);
echo "Query by hour : , affected: ".$d." rows (all row)<br>";
$result = $mysql->execute($g->queryDay($client_id,$device)) ;
$d = mysql_num_rows($result);
echo "Query by Day : , affected: ".$d." rows (all row)<br>";
$result = $mysql->execute($g->queryWeek($client_id,$device)) ;
$d = mysql_num_rows($result);
echo "Query by week : , affected: ".$d." rows (Group by hour)<br>";
$result = $mysql->execute($g->queryMonth($client_id,$device)) ;
$d = mysql_num_rows($result);
echo "Query by Month : , affected: ".$d." rows (Group by hour)<br>";
$result = $mysql->execute($g->queryYear($client_id,$device)) ;
$d = mysql_num_rows($result);
echo "Query by Year : , affected: ".$d." rows (Group By Day)<br>";


echo "<br><h3>Test Case for Graph Data and Sampling (comment when in use na ja)</h3>" ; 
$a =$g->inHour($client_id,$device,$freq);
echo "<h4>by Hour : sampling =".$freq."</h4>" ;
print_r($a);
$a =$g->inDay($client_id,$device,$freq);
echo "<h4>by Day : sampling =".$freq."</h4>" ;
print_r($a);
$a =$g->inWeek($client_id,$device,$freq);
echo "<h4>by Week : sampling =".$freq."</h4>" ;
print_r($a);
$a =$g->inMonth($client_id,$device,$freq);
echo "<h4>by Month : sampling =".$freq."</h4>" ;
print_r($a);
$a =$g->inYear($client_id,$device,$freq);
echo "<h4>by Year : sampling =".$freq."</h4>" ;
print_r($a);
*/
?>